import React from 'react'
// ===================================================================== Temp
import TableModal from '#cpt/temp/TableModal'
// ===================================================================== Declare
const { $http, $Lazy, $fn } = window
// ===================================================================== Lazy
const Input  = $Lazy.load(()=>import('@antd/form/input'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		edit: ''
	}
	
	upRows = []
	
	open = ({header, table_rows, op_key}) => {
		this.cols = []
		this.op_key = op_key
		for (let v in header) {
			this.cols = [...this.cols ,{title: header[v], dataIndex: v, width: 200, render: (text, rows) => this.state.edit !== rows.uuid ? text : <Input p='请输入' value={text} onChange={value=> {rows[v] = value; rows.isChanged = true} } /> }]
		}
		this.modal.open()
		this.table?.table.updateCol(this.cols)
		this.setState({data: table_rows.data})
	}
	
	// 表单
	cols = []
	
	render(){
		return TableModal.call(this,{ title: '导入表格-数据确认', labelWidth:'s7', width:'99%', height:'80%', okText:'确认无误', enter: false,
			table:{ 
				onRow: (rows) => {
					const { edit } = this.state
					rows.uuid !== edit && this.setState({edit: ''})
				},
				onRows: (rows) => {
					this.setState({edit: rows.uuid})
				}
			},
			onOk:()=> {
				const { data } = this.state
				let rows = data.filter(v => v.isChanged)
				if (rows.length !== 0) {
					$http.submit(null, 'temp-import/upRow', {param:{rows, op_key: this.op_key}} ).then(data => {
						$http.submit(null, 'temp-import/import', {param:{op_key: this.op_key}} ).then(data => {
							$fn.msg.success('导入成功！')
							this.modal.close()
						})
					})
					window.onRefresh = () =>{		
						this.modal.close()
					}
				} else {
					$http.submit(null, 'temp-import/import', {param:{op_key: this.op_key}} ).then(data => {
						$fn.msg.success('导入成功！')
						this.modal.close()
					})
					window.onRefresh = () =>{
						this.modal.close()
					}
				}
			}
		})
	}
}

export default Index
 /* 导入表格可视化 2021-8-20  --伍智睿 */